﻿using System.Configuration;
using JTLeigh.Commerce.Spencer.Core.Common.Services;
using JTLeigh.Commerce.Spencer.Core.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JTLeigh.Commerce.Spencer.Core.Common.UnitTests.Services
{

    /// <summary>
    /// Unit tests for the <see cref="ConfigurationService"/> class.
    /// </summary>
    [TestClass]
    public class ConfigurationServiceFixture
    {

        #region Methods

        #region GetSection

        /// <summary>
        /// Ensures <see cref="ConfigurationService.GetSection{TSection}"/> returns the specified
        /// configuration section when it exists.
        /// </summary>
        [TestMethod]
        public void TestGetSection()
        {
            ConfigurationService service;
            ConnectionStringsSection section;
            ConnectionStringSettings connectionString;

            service = new ConfigurationService();
            section = service.GetSection<ConnectionStringsSection>(TestConstants.TenantId.JTLeigh, "connectionStrings");

            Assert.IsNotNull(section);
            connectionString = section.ConnectionStrings["TestConnectionString"];
            Assert.IsNotNull(connectionString);
            Assert.AreEqual("TestConnectionString", connectionString.Name);
            Assert.AreEqual("TestConnectionStringValue", connectionString.ConnectionString);
        }

        /// <summary>
        /// Ensures <see cref="ConfigurationService.GetSection{TSection}"/> throws <see cref="ConfigurationErrorsException"/>
        /// when the specified configuration section does not exist.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ConfigurationErrorsException))]
        public void TestGetSectionWithUnknownSection()
        {
            ConfigurationService service;

            service = new ConfigurationService();
            service.GetSection<ConnectionStringsSection>(TestConstants.TenantId.JTLeigh, "unknownSection");
        }

        /// <summary>
        /// Ensures <see cref="ConfigurationService.GetSection{TSection}"/> throws <see cref="ConfigurationErrorsException"/>
        /// when the specified configuration section's type doesn't match the specified type.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ConfigurationErrorsException))]
        public void TestGetSectionWithMismatchedSectionType()
        {
            ConfigurationService service;

            service = new ConfigurationService();
            service.GetSection<ConnectionStringsSection>(TestConstants.TenantId.JTLeigh, "appSettings");
        }

        #endregion // GetSection

        #endregion // Methods

    }

}
